import toast from './Toast.js'
import store from '../store/index.js'

const handler = (res, resolve) => {
	if (res.code === 403) {
		store.commit('logout')
		uni.navigateTo({
			url: '/pages/login/login'
		})
	} else
		resolve(res)
}

export default {
	request(data) {
		return new Promise((resolve, reject) => {
			uni.request({
				//url: 'http://47.115.30.5:8080/diary' + data.url,
				url: 'http://localhost:10998' + data.url,
				method: data.method,
				header: {
					cookie: store.state.cookie
				},
				data: data.data,
				success: res => {
					handler(res.data, resolve)
				},
				fail(res) {
					console.log(res)
				}
			})
		})
	},
	uploadFile(path) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				// url: 'http://47.115.30.5:8080/diary/file/upload',
				url: 'http://localhost:10998/file/upload',
				header: {
					cookie: store.state.cookie
				},
				name: 'file',
				filePath: path,
				success: res => {
					res = JSON.parse(res.data)
					handler(res, resolve)
				},
				fail: (res) => {
					console.log(res)
				}
			})
		})
	}
}
